package org.gdstash.ui;

import org.gdstash.util.GDImagePool;

import javax.swing.*;
import java.awt.*;

public class GDProgressDialog extends JDialog {
    private Component owner;
    private JLabel lblImage;
    private JProgressBar pbProgress;
    private JLabel lblText;

    public GDProgressDialog(final Frame owner) {
        this(owner, null);
    }

    public GDProgressDialog(final Frame owner, final String text) {
        super(owner, false);
        this.owner = owner;
        final JPanel panel = this.buildPanel();
        this.lblText.setText(text);
        this.getContentPane().add(panel);
        this.pack();
        this.setLocationRelativeTo(owner);
        this.setDefaultCloseOperation(0);
    }

    private JPanel buildPanel() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel panel = new JPanel();
        this.lblImage = new JLabel(GDImagePool.iconProgress);
        (this.pbProgress = new JProgressBar()).setIndeterminate(true);
        (this.lblText = new JLabel()).setFont(fntLabel);
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage).addComponent(this.pbProgress).addComponent(this.lblText));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage)).addGroup(layout.createParallelGroup().addComponent(this.pbProgress)).addGroup(layout.createParallelGroup().addComponent(this.lblText));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblImage, this.pbProgress);
        layout.linkSize(0, this.lblImage, this.lblText);
        layout.linkSize(1, this.pbProgress, this.lblText);
        return panel;
    }

    public void setText(final String text) {
        this.lblText.setText(text);
    }
}
